<template src="./template.html"></template>

<script>
/* global moment */
import ResizeMixin from 'mixins/resize'
import { PAGE_CONFIG, ORDER_SORTS } from 'common/consts';
import { EXPIRED_STATUS } from '../consts';
import { COL_INDEXS } from './consts';
import { getEmployee, getCourseUserLabels } from './models';
import { deleteUser as deleteUserApi } from '../manager/models';

export default {
  mixins: [ResizeMixin],
  data() {
    return {
      COL_INDEXS,
      EXPIRED_STATUS,
      isPageLoading: false,
      userLabels: [],

      tableList: [],
      PAGE_CONFIG: PAGE_CONFIG,
      pageNo: PAGE_CONFIG.defaultPageNo,
      pageSize: PAGE_CONFIG.defaultPageSize,
      totalCount: 0,
      filterForm: {
        // userType: 2,
        q: '',
        enabled: '',
        userLable: '',
        orders: [{
          column: 'createTime',
          order: 'desc'
        }]
      },
    };
  },
  computed: {
    userInfo() {
      return this.$store.state.user.userInfo
    }
  },

  methods: {
    indexMethod(index) {
      return (this.pageNo - 1) * this.pageSize + index + 1;
    },
    $sortChange({ column, prop, order }) {
      console.log(column, prop, order)
      const orderIdx = this.filterForm.orders.findIndex(it => it.column === prop)
      const params = {
        column: prop,
        order: ORDER_SORTS[order]
      }
      if (orderIdx <= -1) {
        if (order) {
          this.filterForm.orders.push(params)
        }
      } else {
        if (order) {
          this.$set(this.filterForm.orders, orderIdx, params)
        } else {
          this.filterForm.orders.splice(orderIdx, 1)
        }
      }
      this.$getTableList()
    },
    $search() {
      this.pageNo = 1
      this.$getTableList()
    },
    $handleRecord(row) {
      this.$router.push({
        name: 'curriculum.manager.record',
        query: {
          userId: row.id,
          nickName: row.nickName
        }
      })
    },
    $handleAdd() {
      this.$router.push('/users/staff/add')
    },
    $handleEdit(row) {
      this.$router.push(`/users/staff/edit/${row.id}`)
    },
    $handleDelete(row) {
      this.$confirm('是否确认删除当前账号？', {
        type: 'seriousWarning',
        confirmButtonText: '删除',
      })
        .then(() => {
          this.deleteUser(row);
        })
        .catch(() => {});
    },

    async deleteUser(row) {
      this.isPageLoading = true;
      const params = {
        ids: row.id,
      };
      try {
        await deleteUserApi(params);
        this.isPageLoading = false;
        this.$message.success('人员信息删除成功！')
        this.$getTableList();
      } catch (err) {
        this.isPageLoading = false;
        this.$message.error(err.message || err.message || '删除人员异常')
      }
    },
    async $getTableList() {
      this.isPageLoading = true;
      const params = {
        ...this.filterForm,
        limit: this.pageSize,
        page: this.pageNo,
        orderType: this.filterForm.orders && this.filterForm.orders[0] && this.filterForm.orders[0].order
      };
      delete params.orders
      try {
        const { data } = await getEmployee(params);
        this.tableList = (data.list || []).map(it => ({
          ...it,
          createTime: moment(it.createTime).format('YYYY-MM-DD HH:mm'),
        }));
        this.totalCount = data.total
        this.isPageLoading = false;
      } catch (err) {
        this.isPageLoading = false;
        this.$message.error(err.message || err.message || '获取员工列表出错')
      }
    },
    $handlePageChange() {
      this.$getTableList()
    },
    async $getCourseUserLabels() {
      const params = {
        courseId: this.courseId,
        limit: 99999,
        page: 1,
      };
      try {
        const { data } = await getCourseUserLabels(params)
        this.userLabels = data || []
      } catch (err) {
        this.$message.error(err.message || '获取分组出错')
      }
    },
    $handleImport() {
      this.$router.push({
        name: 'users.staff.import'
      })
    }
  },
  created() {
    this.$getTableList()
    this.$getCourseUserLabels()
  },
};
</script>
